package service.impl;

import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.PageBean;
import pojo.User;
import service.Uservice;
import util.Sql;

import java.util.List;

public class UserviceImpl implements Uservice {

    //1.创建工厂对象

    @Override
    public List<User> selectuserAll() {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        List<User> userList=mapper.selectuserAll();
        sqlSession.close();
        return userList;
    }

    @Override
    public void addUser(User user) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteByids(int[] ids) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        mapper.deleteByids(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteByid(int id) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        mapper.deleteByid(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public User selectLogin(User user) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        User user1=mapper.selectLogin(user);
        sqlSession.commit();
        sqlSession.close();
        return user1;
    }

    @Override
    public PageBean<User> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        //开始索引
        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        List<User> rows=mapper.selectByPage(begin,size);
        int Count =mapper.selectCount();
        //封装对象
        PageBean<User> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(Count);
        sqlSession.close();
        return pageBean;
    }

    @Override
    public PageBean<User> selectByPageAndCondition(int currentPage, int pageSize, User user) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        //开始索引
        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        String name= user.getName();
        if(name!=null && name.length()>0){
            user.setName("%"+name+"%");
        }

        List<User> rows=mapper.selectByPageAndCondition(begin,size,user);
        int Count =mapper.selectCountByCondition(user);
        //封装对象
        PageBean<User> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(Count);
        sqlSession.close();
        return pageBean;
    }

    @Override
    public int updateplus(User user) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        int count=mapper.updateplus(user);
        sqlSession.commit();
        sqlSession.close();
        return count;
    }

    @Override
    public User selectById(int id) {
        SqlSession sqlSession = Sql.openSeeion();
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        User user=mapper.selectById(id);
        sqlSession.close();
        return user;
    }
}
